package gw.demo2.controller;

import gw.demo2.entity.Merchant;
import gw.demo2.entity.Product;
import gw.demo2.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/merchant")
public class MerchantController {
    @Autowired
    private MerchantService merchantService;

    @PostMapping("/login")
    public ResponseEntity<?> merchantLogin(@RequestBody Map<String, Object> request) {
        int merchantId = Integer.parseInt(request.get("merchantId").toString());
        String password = request.get("password").toString();

        Merchant merchant = merchantService.login(merchantId, password);
        if (merchant != null) {
            return ResponseEntity.ok().body(merchant);
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid credentials");
        }
    }

    @PostMapping("/register")
    public ResponseEntity<?> merchantRegister(@RequestBody Map<String, Object> request) {
        String nickname = request.get("nickname").toString();
        String password = request.get("password").toString();

        boolean registered = merchantService.register(nickname, password);
        if (registered) {
            // 获取刚注册的商家ID
            int merchantId = merchantService.getMerchantIdByNickname(nickname);

            RegisterResponse response = new RegisterResponse();
            response.setSuccess(true);
            response.setMessage("Merchant registered successfully");
            response.setId(merchantId);

            return ResponseEntity.ok().body(response);
        } else {
            return ResponseEntity.status(HttpStatus.CONFLICT).body("Nickname already exists");
        }
    }

    @PutMapping("/profile")
    public ResponseEntity<?> updateProfile(@RequestBody Map<String, Object> request) {
        int merchantId = Integer.parseInt(request.get("merchantId").toString());
        String newNickname = request.get("newNickname").toString();
        String newPassword = request.get("newPassword").toString();

        if (merchantService.updateMerchantInfo(merchantId, newNickname, newPassword)) {
            return ResponseEntity.ok().body("Profile updated successfully");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Merchant not found");
        }
    }

    @PostMapping("/products")
    public ResponseEntity<?> addProduct(@RequestBody Product product) {
        if (merchantService.addProduct(product)) {
            return ResponseEntity.ok().body("Product added successfully");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to add product");
        }
    }
    @GetMapping("/profile")
    public ResponseEntity<?> getMerchantProfile(@RequestParam int merchantId) {
        Merchant merchant = merchantService.selectMerchantById(merchantId);
        if (merchant != null) {
            return ResponseEntity.ok().body(merchant);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Merchant not found");
        }
    }
    @GetMapping("/products")
    public ResponseEntity<?> getProducts(@RequestParam int merchantId) {
        return ResponseEntity.ok().body(merchantService.getProductsByMerchantId(merchantId));
    }

    @GetMapping("/purchase-records")
    public ResponseEntity<?> getPurchaseRecords(@RequestParam int merchantId) {
        return ResponseEntity.ok().body(merchantService.getPurchaseRecordsByMerchantId(merchantId));
    }
}